local blaze = require "blaze"
local app = require "app"
local game = app.game.running

local M = game.ui.element()

local log = game.logging.get("seats")

M.store_watchers = {
    room = {
        ["$"] = function(self, store, state)
            self:refresh(state.players)
        end,

        ["玩家进入"] = function(self, store, state, player)
            self:refresh(state.players)
        end,

        ["玩家离开"] = function(self, store, state, player)
            self:refresh(state.players)
        end
    },

    gameplay = {
        ["阶段变更"] = function(self, store, state)
            if state.phase_id == game.util.consts.PHASE_PAYOUT then
                self:refresh(game.store.room.state.players)
            end
        end
    }
}

function M:ctor(count, res)
    assert(res)
    self.res = res
    self.count = count

    local Seat = require "club.threecard.baccaratlib.ui.widgets.Seat"
    for i = 1, self.count do
        self[i] = Seat.new(i)
        self[i]:set_data(nil)
    end
end

function M:on_load()
    self:super("on_load")

    for i = 1, self.count do
        self[i]:load()
        local root = self.transform:Find("seat_"..i)
        if root then
            self[i]:set_parent(root, false)
        else
            log.error("初始化座位信息错误  座位号："..i)
        end
    end
end

function M:refresh(players)
    local t = {}
    for no, player in pairs(players) do
        table.insert(t, player)
    end

    table.sort(t, function(p1, p2) return p1.seat.no < p2.seat.no end)

    for i = 1, self.count do
        local seat = self[i]
        if seat then
            local player = t[i]
            seat:set_data(player)
            seat:mark_rich(99)
            seat:mark_luck(false)
        end
    end

    do return end

    table.sort(t, function(p1, p2) return p1.chip > p2.chip end)
    for i = 1, self.count-1 do
        local seat = self[i]
        if seat then
            local player = t[i]
            if player then
                seat:set_data(player)
                seat:mark_rich(i)
                seat:mark_luck(false)
            else
                seat:set_data(nil)
            end
        end
    end

    table.sort(t, function(p1, p2) 
        local p1_rate = (p1.bet_times > 0) and (p1.win_times/p1.bet_times) or 0
        local p2_rate = (p2.bet_times > 0) and (p2.win_times/p2.bet_times) or 0

        if p1_rate == p2_rate then
            if p1.bet_times == p2.bet_times then
                return p1.chip > p2.chip
            else
                return p1.bet_times > p2.bet_times
            end
        else
            return p1_rate > p2_rate
        end
     end)
    local luckiest_player = t[1]
    local seat = self[self.count]
    if seat then
        seat:set_data(luckiest_player)
        seat:mark_luck(true)
        seat:mark_rich(99)
    end
end

function M:find(player)
    for i = 1, self.count do
        local seat = self[i]
        if seat.player == player then
            return seat
        end
    end

    return nil
end

return M